package com.jeff.tianti.controller;

import com.jeff.tianti.cms.dto.AdvSpaceQueryDTO;
import com.jeff.tianti.cms.dto.FriendShipLinkDTO;
import com.jeff.tianti.cms.entity.AdvSpace;
import com.jeff.tianti.cms.entity.FriendShipLink;
import com.jeff.tianti.cms.service.AdvSpaceService;
import com.jeff.tianti.cms.service.FriendShipLinkService;
import com.jeff.tianti.common.dto.AjaxResult;
import com.jeff.tianti.common.entity.PageModel;
import com.jeff.tianti.org.dto.DictionaryDTO;
import com.jeff.tianti.org.entity.Dictionary;
import com.jeff.tianti.org.service.DictionaryService;
import com.jeff.tianti.util.Constants;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/advSpace")
public class AdvSpaceController {
    @Autowired
    AdvSpaceService advSpaceService;
    @Autowired
    DictionaryService dictionaryService;

    @Autowired
    FriendShipLinkService friendShipLinkService;

    /**
     * 获取 友联列表
     *
     * @return
     */
    @RequestMapping("/list")
    public String index(HttpServletRequest request, Model model) {
        String dicCode = request.getParameter("dicCode");
        String currentPageStr = request.getParameter("currentPage");
        String pageSizeStr = request.getParameter("pageSize");
        String pDicCode = request.getParameter("pDicCode");

        DictionaryDTO dictionaryDTO = new DictionaryDTO();
        dictionaryDTO.setCode(pDicCode);
        dictionaryDTO.setCodeLike(false);
        List<Dictionary> parentAdvDictionaryList = dictionaryService.queryDicList(dictionaryDTO);
        List<Dictionary> advDictionaryList = null;
        if (!CollectionUtils.isEmpty(parentAdvDictionaryList)) {
            String dicId = parentAdvDictionaryList.get(0).getId();
            DictionaryDTO dto = new DictionaryDTO();
            dto.setPid(dicId);
            advDictionaryList = dictionaryService.queryDicList(dto);
        }
        int currentPage = 1;
        int pageSize = 10;
        if (StringUtils.isNotBlank(currentPageStr)) {
            currentPage = Integer.parseInt(currentPageStr);
        }
        if (StringUtils.isNotBlank(pageSizeStr)) {
            pageSize = Integer.parseInt(pageSizeStr);
        }

        AdvSpaceQueryDTO advSpaceQueryDTO = new AdvSpaceQueryDTO();
        advSpaceQueryDTO.setDicCode(dicCode);
        advSpaceQueryDTO.setCurrentPage(currentPage);
        advSpaceQueryDTO.setPageSize(pageSize);

        PageModel<AdvSpace> page = advSpaceService.queryAdvSpacePage(advSpaceQueryDTO);
        List<AdvSpace> list = page.getList();
        for (int i = 0; i < list.size(); i++) {
            AdvSpace advSpace = list.get(i);
            String dicCode1 = advSpace.getDicCode();
            DictionaryDTO dictionary = new DictionaryDTO();
            dictionary.setCodeLike(false);
            dictionary.setCode(dicCode1);
            List<Dictionary> dictionaries = dictionaryService.queryDicList(dictionary);
            if (!CollectionUtils.isEmpty(dictionaries)){
                String name = dictionaries.get(0).getName();
                advSpace.setDicName(name);
            }
        }
        model.addAttribute("page", page);
        model.addAttribute("advSpaceQueryDTO", advSpaceQueryDTO);
        model.addAttribute("dicCode", dicCode);
        model.addAttribute("advDictionaryList", advDictionaryList);
        model.addAttribute(Constants.MENU_NAME, Constants.MENU_ADV_SPACE);
        return "/cms/adv/adv_space_list";
    }

    /**
     * 跳转到友情编辑页
     *
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("/edit")
    public String edit(HttpServletRequest request, Model model) {
        String pDicCode = request.getParameter("pDicCode");
        String id = request.getParameter("id");
        AdvSpace advSpace = advSpaceService.find(id);

        DictionaryDTO dictionaryDTO = new DictionaryDTO();
        dictionaryDTO.setCode(pDicCode);
        dictionaryDTO.setCodeLike(false);
        List<Dictionary> parentAdvDictionaryList = dictionaryService.queryDicList(dictionaryDTO);
        List<Dictionary> advDictionaryList = null;
        if (!CollectionUtils.isEmpty(parentAdvDictionaryList)) {
            String dicId = parentAdvDictionaryList.get(0).getId();
            DictionaryDTO dto = new DictionaryDTO();
            dto.setPid(dicId);
            advDictionaryList = dictionaryService.queryDicList(dto);
        }
        model.addAttribute("advDictionaryList", advDictionaryList);
        model.addAttribute("advSpace", advSpace);

        return "/cms/adv/dialog/adv_space_edit";
    }

    /**
     * 栏目保存操作
     *
     * @param request
     * @return
     */
    @RequestMapping("/ajax/save")
    @ResponseBody
    public AjaxResult doFriendShipAjaxSave(HttpServletRequest request) {
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setSuccess(false);
        try {
            String id = request.getParameter("id");
            String dicCode = request.getParameter("dicCode");
            String title = request.getParameter("title");
            String content = request.getParameter("content");
            String advUrl = request.getParameter("advUrl");
            String picUrl = request.getParameter("picUrl");
            String orderNo = request.getParameter("orderNo");
            String channel = request.getParameter("channel");
            AdvSpace advSpace = new AdvSpace();
            advSpace.setId(id);
            advSpace.setDicCode(dicCode);
            advSpace.setTitle(title);
            advSpace.setContent(content);
            advSpace.setAdvUrl(advUrl);
            advSpace.setUrl(picUrl);
            advSpace.setOrderNo(orderNo);
            advSpace.setChannel(channel);
            advSpace.setDeleteFlag(AdvSpace.DELETE_FLAG_NORMAL);
            if (StringUtils.isNotBlank(id)) {
                advSpaceService.updateSelective(advSpace);
            } else {
                advSpaceService.save(advSpace);
            }
            ajaxResult.setSuccess(true);
        } catch (Exception e) {
            ajaxResult.setMsg(e.getMessage());
            e.printStackTrace();
        }

        return ajaxResult;
    }

    /**
     * 栏目保存操作
     *
     * @param request
     * @return
     */
    @RequestMapping("/ajax/del")
    @ResponseBody
    public AjaxResult doFriendShipAjaxDel(HttpServletRequest request) {
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setSuccess(false);
        try {
            String id = request.getParameter("id");
            advSpaceService.delete(id);
            ajaxResult.setSuccess(true);
        } catch (Exception e) {
            ajaxResult.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return ajaxResult;
    }

}
